Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds screenshot options to context menu and commands #27083

Open
wants to merge 51 commits into
base: master
Choose a base branch
from

Conversation

jonathansampson
Copy link
Contributor

@jonathansampson jonathansampson commented Dec 21, 2024

Resolves brave/brave-browser#25816
Security/Privacy Review: https://github.com/brave/reviews/issues/1831

screenshot-context-menu.mp4

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

  1. Right-click on a page
  2. Click Screenshot Tools from the context menu
  3. Select an option (i.e., Selection, Viewport, Full page) from the submenu

Once a screenshot has been taken, you should see a UI element (with a preview of your screenshot) informing you that the screenshot has been copied to your device's clipboard.

@jonathansampson jonathansampson added enhancement CI/skip-android Do not run CI builds for Android CI/skip-ios Do not run CI builds for iOS labels Dec 21, 2024
@jonathansampson jonathansampson self-assigned this Dec 21, 2024
@github-actions github-actions bot added the CI/run-upstream-tests Run upstream unit and browser tests on Linux and Windows (otherwise only on Linux) label Dec 21, 2024
@jonathansampson jonathansampson marked this pull request as ready for review December 23, 2024 04:48
@jonathansampson jonathansampson requested review from a team and bridiver as code owners December 23, 2024 04:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch 2 times, most recently from 0e69a8a to 4b7451d Compare December 28, 2024 23:18
@jonathansampson jonathansampson marked this pull request as ready for review December 29, 2024 02:58
@jonathansampson
Copy link
Contributor Author

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

@bridiver
Copy link
Collaborator

bridiver commented Jan 2, 2025

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

Make sure you follow the latest updates using TabInterface and not WebContentsUserData

@jonathansampson
Copy link
Contributor Author

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

Make sure you follow the latest updates using TabInterface and not WebContentsUserData

Looking into that now. Thanks!

@jonathansampson jonathansampson marked this pull request as draft January 2, 2025 21:17
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch 2 times, most recently from 22868e1 to fdd6104 Compare January 4, 2025 17:34
@github-actions github-actions bot added the CI/run-network-audit Run network-audit label Jan 4, 2025
Copy link
Contributor

@fallaciousreasoning fallaciousreasoning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % nits

@jonathansampson jonathansampson marked this pull request as ready for review January 7, 2025 14:04
By using a browser reference we can avoid the reliance on global methods to look up a window from a given webcontents instance.
Removed the helper "TakeScreenshot" method which originally existed to stand up and instance of the helper. Now that the helper is a proper tab feature, we can use it as such from context menu commands and elsewhere.
If there is nothing selected, we shouldn't attempt to construct an autocomplete match, run the autocomplete classifier, etc. Furthermore, this enables us to simplify tests involving the context menu due to reduced setup.
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch from f8c8d02 to 6d39b1e Compare January 15, 2025 17:02
After rebasing, there is now a failure in this PR unrelated to the contents or changes introduced by the PR itself. A fresh build from scratch will resolve this, but a component build will proceed if any change is made to the policy template file. As such, a typo has been found and fixed so as to 1) Improve the comments, and importantly 2) Fix the build 😀 See https://bravesoftware.slack.com/archives/C7VLGSR55/p1736928018175489?thread_ts=1736911378.517619&cid=C7VLGSR55 for more info.
]

deps = [
"//chrome/browser/image_editor:image_editor",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be just //chrome/browser/image_editor same as "//chrome/browser/ui and //ui/base/clipboard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I purposefully provide the target names, just to more clearly communicate intent. If it's okay, I'd prefer to keep them explicit.

browser/brave_screenshots/screenshots_tab_feature.h Outdated Show resolved Hide resolved
browser/brave_screenshots/screenshots_tab_feature.cc Outdated Show resolved Hide resolved
browser/brave_screenshots/screenshots_tab_feature.cc Outdated Show resolved Hide resolved
browser/brave_screenshots/screenshots_tab_feature.cc Outdated Show resolved Hide resolved
browser/brave_screenshots/screenshots_tab_feature.cc Outdated Show resolved Hide resolved
browser/brave_screenshots/strategies/fullpage_strategy.cc Outdated Show resolved Hide resolved
Copy link
Contributor

[puLL-Merge] - brave/brave-core@27083

Description

This PR introduces screenshot functionality to the Brave browser, allowing users to capture full-page, viewport, or selected area screenshots directly from the browser's context menu. The feature is gated behind a feature flag and includes new command IDs, UI strings, and implementation files for different screenshot strategies.

Changes

Changes

  1. app/brave_command_ids.h:

    • Added new command IDs for screenshot tools and specific screenshot actions.
  2. app/brave_generated_resources.grd and app/brave_screenshots_strings.grdp:

    • Added new string resources for screenshot menu items and options.
  3. browser/about_flags.cc:

    • Added a new flag "brave-screenshots" to enable/disable the screenshot feature.
  4. browser/brave_screenshots/:

    • Added new files for screenshot feature implementation, including strategies for full-page, viewport, and selection screenshots.
  5. browser/ui/brave_browser_command_controller.cc:

    • Updated to handle new screenshot commands.
  6. browser/ui/browser_commands.cc and browser/ui/browser_commands.h:

    • Added a new function TakeScreenshot to handle screenshot actions.
  7. chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc:

    • Modified to include the new screenshot menu items in the context menu.
  8. chromium_src/chrome/browser/ui/tabs/tab_features.cc:

    • Added initialization for the Brave screenshots feature.
sequenceDiagram
    participant User
    participant ContextMenu
    participant BraveScreenshotsTabFeature
    participant ScreenshotStrategy
    participant DevTools
    participant Clipboard

    User->>ContextMenu: Right-click and select screenshot option
    ContextMenu->>BraveScreenshotsTabFeature: StartScreenshot
    BraveScreenshotsTabFeature->>ScreenshotStrategy: Create strategy
    ScreenshotStrategy->>DevTools: Capture screenshot (if full-page)
    DevTools-->>ScreenshotStrategy: Return screenshot data
    ScreenshotStrategy-->>BraveScreenshotsTabFeature: Return capture result
    BraveScreenshotsTabFeature->>Clipboard: Copy image to clipboard
    BraveScreenshotsTabFeature->>User: Display screenshot bubble
Loading

Possible Issues

  1. The full-page screenshot strategy uses DevTools protocol, which might have performance implications for very large pages.
  2. The feature is currently limited to desktop platforms (Windows, macOS, Linux).

Security Hotspots

  1. browser/brave_screenshots/strategies/fullpage_strategy.cc:

    • Uses DevTools protocol to capture full-page screenshots. Ensure proper sanitization of input and output to prevent potential security vulnerabilities.
  2. browser/brave_screenshots/screenshots_utils.cc:

    • Handles copying images to the clipboard. Ensure proper handling of large images to prevent potential memory issues or crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/run-network-audit Run network-audit CI/run-upstream-tests Run upstream unit and browser tests on Linux and Windows (otherwise only on Linux) CI/skip-android Do not run CI builds for Android CI/skip-ios Do not run CI builds for iOS enhancement puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include "Capture Screenshot" option in the context menu
4 participants